<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class search_controller extends PZ_Controller{
	public function __construct(){
		parent::__construct();
	}

	protected function init(){
		$api = load_api('setting');
		$setting = $api->getSetting('optimizer');
		$this->likesearch = $setting['like_search'];
		$keyword= $this->param('keyword');
		$this->keyword = safeEncoding($keyword);
		$this->page = $this->param('page','1');
		$this->seo_title($keyword);
		$this->seo_keyword($keyword);
		$this->seo_description($keyword);

		if($this->keyword){
			if($this->likesearch){
				$conditions['likekeyword'] = $this->keyword;
			}else{
				$conditions['keyword'] = pzxencode($this->keyword);
			}
			$this->condition = $conditions;
		}

		$setting = $api->getSetting('ads');
		$ads = $setting['detailpage_ad'];
		$ad = rand_pop($setting['pinpage_ad']);
		$ads[] = $ad;
		$this->ads = $ads;
	}

	public function pin(){
		if($this->keyword){
			$api = load_api('setting');
			$setting = $api->getSetting('layoutpin');
			$pagesize = $setting['uipin_numperpage']?$setting['uipin_numperpage']:15;

			$args = array("page"=>"2","wf"=>"1");
			$args['keyword']=$this->keyword;
			$shareapi = load_api('share');
			$shares = $shareapi->search($this->condition,$this->page,$pagesize," ptx_share.create_time DESC ");
			$shares = $shareapi->prepareShares($shares);
			if($setting['uipin_ads']){
				$shares = $shareapi->add_ads($shares);
			}
			$pager = &load_class('Pager', 'core');
			$this->nextpage_url = site_url_param('search/pin',$args);
			$this->pages = $pager->createPages('search/pin',$args);
			$this->tpl_waterfall = $this->render('waterfall/pin',array('shares'=>$shares));
			$this->pinsetting = $setting;
			$this->output("search/pin",$data,($this->param('wf'))?false:true);
		}else{
			$this->output("search/pin");
		}
	}

	public function user(){
		if($this->keyword){
			$api = load_api('setting');
			$setting = $api->getSetting('layoutalbum');
			$pagesize = $setting['uialbum_numperpage']?$setting['uialbum_numperpage']:20;

			$args = array("page"=>"2","wf"=>"1");
			$args['keyword']=$this->keyword;
			$userdb = load_db('user');
			$users = $userdb->searchWithShare($this->condition,$this->page,$pagesize,null,' ptx_user.create_time DESC ');
			$pager = &load_class('Pager', 'core');
			$this->nextpage_url = site_url_param('search/user',$args);
			$this->pages = $pager->createPages('search/user',$args);
			$user_id = $this->islogin?$this->cuser['uid']:0;
			$users = $this->addrelation($users,$user_id);
			$this->tpl_waterfall = $this->render('waterfall/user',array('users'=>$users));
			$this->layoutsetting = $setting;
			$this->output("search/user",$data,($this->param('wf'))?false:true);
		}else{
			$this->output("search/user");
		}
	}

	private function addrelation($friends,$user_id){
		foreach ($friends as $key=>$friend){
			$friends[$key]['relation_view'] = $this->relationView($user_id,$friend['user_id']);
		}
		return $friends;
	}

	public function topic(){
		if($this->keyword){
			$api = load_api('setting');
			$setting = $api->getSetting('layoutalbum');
			$pagesize = $setting['uialbum_numperpage']?$setting['uialbum_numperpage']:20;
			$args = array("page"=>"2","wf"=>"1");
			$args['keyword']=$this->keyword;
			$topicapi = load_api('topic');
			$topics = $topicapi->searchFull($this->condition,$this->page,$pagesize,false);
			$pager = &load_class('Pager', 'core');
			$this->nextpage_url = site_url_param('search/topic',$args);
			$this->pages = $pager->createPages('search/topic',$args);
			$this->tpl_waterfall = $this->render('waterfall/storetopic',array('topics'=>$topics));
			$this->output("search/topic",$data,($this->param('wf'))?false:true);
		}else{
			$this->output("search/topic");
		}
	}

	public function group(){
		if($this->keyword){
			$pagesize = 20;
			$args['keyword']=$this->keyword;
			$groups = load_db("group")->search($this->condition,$this->page,$pagesize,null," ptx_group.create_time DESC ");
			$pager = &load_class('Pager', 'core');
			$this->pages = $pager->createPages('search/group',$args);
			$ptx_share = load_db('share');
			foreach ($groups as $key=>$group){
				$share_conditions['keyword'] = 'group'.$group['group_id'].'g';
				$shares = $ptx_share->count($share_conditions);
				$groups[$key]['total_share'] = $shares;
			}
			$this->groups = $groups;
			$this->output("search/group");
		}else{
			$this->output("search/group");
		}
	}

	public function store(){
		if($this->keyword){
			$pagesize = 10;
			$args['keyword']=$this->keyword;
			$stores = load_db("store")->search($this->condition,$this->page,$pagesize,null," ptx_store.create_time DESC ");
			$pager = &load_class('Pager', 'core');
			$this->pages = $pager->createPages('search/store',$args);
			if($stores){
				$stores = load_api('store')->preparestores($stores);
				if($this->islogin){
					$favdb = load_db("store/favstore");
					foreach ($stores as $key=>$store){
						$stores[$key]['liked'] = $favdb->find_one(array('user_id'=>$this->cuser['uid'],'store_id'=>$store['store_id']));
					}
				}
			}
			$this->stores = $stores;
			$this->output("search/store");
		}else{
			$this->output("search/store");
		}
	}
}
